<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" content="ie=edge"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>会员级别管理</title>
<#include "../common/easyui-html5.ftl"/>
</head>
<body class="easyui-layout">
	 <div data-options="region:'center'">
		<table id="dg"></table>
	</div>
	<!-- 工具条  -->
	<div id="tb" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<table width="100%">
			<tr width="100%">
				<td width="50%">
					名称: <input class="easyui-textbox" style="line-height:12px; width:200px" id="search_Name"/>
					<!--
					状态:<select class="easyui-combobox" data-options="editable:false,panelHeight:'auto'" style="width:100px" id="role_status">
						<option value="0">不限</option>
						<option value="1">有效</option>
						<option value="2">待审核</option>
						<option value="3">锁定</option>
					</select> -->
					<a href="#"  class="easyui-linkbutton" iconCls="icon-search" onclick="searchFn()">查询</a>
				</td>
				<td align="right" width="50%">
					<a id="addBtn" href="#" class="easyui-linkbutton" iconCls="add_btn" plain="true" onclick="addFn()">增加</a>
				</td>
			</tr>
		</table>
		</div>
	</div>
	<!-- 弹出窗口  -->
	<div id="w" class="easyui-window"
		data-options="modal:true,closed:true,iconCls:'icon-save',
		collapsible:false,minimizable:false,maximizable:false,resizable:false"
			style="width:500px;height:420px;padding:10px;">
			<div class="easyui-layout" data-options="fit:true">

					<div data-options="region:'center',border:false" style="padding:10px;">
                        <form id="form1" method="post">
                            <input type="hidden" name="isAjax" value="1"/>
                        <table>
                            <tr style="display: none;">
                                <td>id:</td>
                                <td><input  type="text" id="form_id" name="id"></input></td>
                            </tr>
                            <tr  >
                                <td>名称</td>
                                <td><input  type="text" id="rankName" name="rankName" class="easyui-textbox" data-options="required:true" style="width:300px"></input></td>
                            </tr>

                            <tr  >
                                <td>积分下限:</td>
                                <td><input  type="text" id="minPoints" name="minPoints" class="easyui-numberbox" data-options="required:true" style="width:300px"></input></td>
                            </tr>
                            <tr  >
                                <td>积分上限:</td>
                                <td><input  type="text" id="maxPoints" name="maxPoints" class="easyui-numberbox" data-options="required:true" style="width:300px"></input></td>
                            </tr>
                            <tr  >
                                <td>在商品详情页显示<br/>该会员等级的商品价格:</td>
                                <td>
                                    <select id="showPrice" name="showPrice" class="easyui-combobox" data-options="editable:false,panelHeight:true"   style="width:300px;">
                                        <option value="0">不显示</option>
                                        <option value="1">显示</option>
                                    </select>
								</td>
                            </tr>
                            <tr  >
                                <td>特殊会员组:</td>
                                <td>
                                    <select id="specialRank" name="specialRank" class="easyui-combobox" data-options="editable:false,panelHeight:true"   style="width:300px;">
                                        <option value="0">否</option>
                                        <option value="1">是</option>
                                    </select>
                                </td>
                            </tr>
                            <tr  >
                                <td>&nbsp;</td>
                                <td   >特殊会员组的会员不会随着积分的变化而变化。</td>
                            </tr>
                            <tr  >
                                <td>初始折扣率:</td>
                                <td ><input  type="text" id="discount" name="discount" class="easyui-numberbox" data-options="min:0,max:100,required:true" style="width:300px"></input></td>
                            </tr>
                            <tr  >
                                <td>&nbsp;</td>
                                <td  >请填写为0-100的整数,如填入80，表示初始折扣率为8折。</td>
                            </tr>
                            <#--<tr  >-->
                                <#--<td>排序</td>-->
                                <#--<td><input  type="text" id="sortOrder" name="sortOrder" class="easyui-numberspinner" data-options="editable:true,value:100,max:999" style="width:300px"></input></td>-->
                            <#--</tr>-->


                        </table>
                        </form>
					</div>
					<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
						<a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" onclick="javascript:submitForm()">保存</a>
						<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" onclick="javascript:closeWinFn()">取消</a>
					</div>

			</div>
	</div>
<script type="text/javascript">
var doPage =  authIds.indexOf(",admin_userrank_dopage,")>=0?true:false;
var doAddBtn =   authIds.indexOf(",admin_userrank_doadd,")>=0?true:false;
var doupdateBtn =  authIds.indexOf(",admin_userrank_doupdate,")>=0?true:false;
var dodelBtn =  authIds.indexOf(",admin_userrank_dodel,")>=0?true:false;
var saveType="add";
$(function(){
			if(!doAddBtn){
				$("#addBtn").hide();
			}
			$('#dg').datagrid({
			 	border:false,
				rownumbers:true,
				checkOnSelect:true,
				fitColumns:true,
				pagination:true,
				singleSelect:true,
				fit:true,
				pageSize:10,
				method:'get',
			  	url:'${rc.contextPath}/admin/userrank/dopage?d='+new Date().getTime(),
			  	queryParams:{ },
			  	onBeforeLoad:function(param){
                    if(!doPage){
                        $.messager.alert('系统提示','您没有权限访问!','info');
                        return;
                    }
					param['pageno'] =  param['page']-1;
					param['pagesize']  = param['rows'];
			  		return true ;
			  	},
			  	onDblClickRow:function(rowIndex, rowData){
			  			//updateRowIndex = rowIndex;
						//getRoleInfoById(rowData['roleId']);
			  	},
			  	onLoadError:function(){
			  	},

				columns:[[
					{field:'id',title:'id',hidden:true},
					{field:'rankName',title:'名称' ,width:100},
                    {field:'minPoints',title:'积分下限' ,width:100},
                    {field:'maxPoints',title:'积分上限' ,width:100},
                    {field:'discount',title:'初始折扣率(%)' ,width:100},
                    {field:'specialRank',title:'特殊会员组' ,width:100,formatter:function(value,row,index){
                        if(value == 1){
                            return "是";
                        }else  if(value == 0){
                            return "否";
                        }
                    }},
                    {field:'showPrice',title:'显示价格' ,width:100,formatter:function(value,row,index){
                        if(value == 1){
                            return "显示";
                        }else  if(value == 0){
                            return "不显示";
                        }
                    }},
					{field:'opt',title:'操作' ,width:100,formatter:optFormat}
				]],
				toolbar: '#tb',
				loadFilter:function(data){
				   if(data.statusCode == normalStatusCode){
                        	var obj = {};
                            obj.total =  data.entity.totalRows;
                            obj.rows = $.isArray(data.entity.pageData)?data.entity.pageData:[];
                            return obj;
				   }else {
				            var obj = {};
                           obj.total = 0;
                            obj.rows = [];
                            return obj;
				   }

				}
			});

			var pager = $('#dg').datagrid("getPager");
			pager.pagination({
				'onBeforeRefresh':function(){
					searchFn();
					return false;
				}
			});
		});

		function searchFn(){
			if(!doPage){
				 $.messager.alert('系统提示','您没有权限访问!','info');
				return;
			}
			var search_Name = $("#search_Name").val();
			//var role_status = $('#role_status').combobox('getValue');
			$('#dg').datagrid('load',{
					//status:role_status,
                rankName:search_Name,
					d:new Date().getTime()
			});

		}
		var resource_dg_loadSuccess = false;
		var updateRowIndex = -1;
		function addFn(){
			 saveType = "add";
            $('#form1').form('reset');
 			 $('#w').window('open').panel('setTitle',"增加供应商");

		}
		function delFn(id){
			$.messager.confirm('系统提示', '您确定要删除该条记录吗?', function(r){
			if (r){
					var url = "${rc.contextPath}/admin/userrank/dodel";
		  	 		$.ajax({
			  	  	  type:'post',
					  url: url,
					  context: document.body,
					  data:{
                          id: id,
					  	d:new Date().getTime()
					  },
					  success:function(data){

					  	if(data.statusCode == normalStatusCode){
							var row = null;
							var rows = $("#dg").datagrid("getRows");
							for(var i=0;i<rows.length;i++){
								if(rows[i]['id'] == id){
									row = rows[i];
									var rowIndex = 	$('#dg').datagrid("getRowIndex",row);
									$('#dg').datagrid("deleteRow",rowIndex);
									break;
								}
							}
					  		$.messager.alert('系统提示','删除成功!','info');

					  	}else{
					  		$.messager.alert('系统提示','删除失败，请刷新后重试!','error');
					  	}
					  },
					  error:function(xhr,textStatus,errorThrown){
					  	var responseText = xhr.responseText;
					  	$.messager.alert('系统提示','删除失败，请刷新后重试!','error');

					  }
				});

				}
			});
		}
		function optFormat (value,row,index){
			var btns = [];
			if(doupdateBtn){
				btns.push('<a class="easyui-linkbutton l-btn l-btn-plain" onclick="updateFn(\''+row['id']+'\')" href="#" plain="true" iconCls="update_btn"><span class="l-btn-left"><span class="l-btn-text update_btn l-btn-icon-left">修改</span></span></a>');
			}
			if(dodelBtn){
				btns.push('<a class="easyui-linkbutton l-btn l-btn-plain" onclick="delFn(\''+row['id']+'\')" href="#" plain="true" iconCls="del_btn"><span class="l-btn-left"><span class="l-btn-text del_btn l-btn-icon-left">删除</span></span></a>');
			}
			return btns.join("&nbsp;");
		}
		function updateFn(id){
			updateRowIndex = -1;
			var rows = $("#dg").datagrid("getRows");
			var row = null;
			for(var i=0;i<rows.length;i++){
				if(rows[i]['id'] == id){
					row = rows[i];
					break;
				}
			}

			var rowIndex =  $('#dg').datagrid("getRowIndex",row);
			 updateRowIndex = rowIndex;
            saveType = 'update'
            $('#form1').form('reset');
            $('#form1').form('load',row);

            $('#w').window('open').panel('setTitle',"修改");

		}

		function submitForm(){
			 var isValidate = $('#form1').form('validate');
			 if(!isValidate){
			 	$.messager.alert('系统提示','请确保填写正确!','warning');
			 	return;
			 }
			var form_id = $("#form_id").val();
            var f_name = $.trim($("#rankName").textbox("getValue"));


			if(f_name.length ==0 ){
				$.messager.alert('系统提示','名称不能为空!','warning');
				return;
			}

            var url =  "${rc.contextPath}/admin/userrank/doadd";
			if(saveType == 'add'){

			}else if(saveType == 'update'){
                url =  "${rc.contextPath}/admin/userrank/doupdate";
			}

            $('#form1').form('submit', {
                        url:url,
				onSubmit: function(param){
						//param.p1 = 'value1';
						//param.p2 = 'value2';

                    var discount = $('#discount').numberbox('getValue');
					if(discount.length > 0){
                        if(discount*1 > 100 || discount*1 <0){
                            $.messager.alert('系统提示','折扣率应该为0-100之间的数字','warning');
                            return false;
                        }
					}

                    jQuery.showMask($("#w")[0],"正在保存中 ....");
					return true;

				},
                success: function(data){
                    jQuery.hideMask($("#w")[0]);
                    data = eval("("+data+")");
                    if(data.statusCode == normalStatusCode){
                        $('#dg').datagrid('reload');
                        $.messager.alert('系统提示','保存成功!','info',closeWinFn);
                    }else{
                        var errorEntity = data.entity;
                        if(errorEntity){
                            $.messager.alert('系统提示','保存失败,'+errorEntity.errorMsg,'warning');
                        }else{
                            $.messager.alert('系统提示','保存失败!','warning');
                        }
                    }
                  //  var data = eval('(' + data + ')');  // change the JSON string to javascript object
                }
        });
		}

		function closeWinFn(){
			$('#w').window('close');
		}


	</script>
</body>
</html>